<?php
namespace app\manage\model;
use think\Db;
use think\Model;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/5
 * Time: 16:25
 */
class HousingInformation extends Model
{
    /**
     * 住房管理
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingList($matching=''){

        $data = input();
        $create_id= create_id($data['token']);
        //单元楼数据
        $data=Db::name('unit_building')
            ->field('tung_num,unit_num,layer_num,floor_type')
            ->where(['u_type'=>1])
            ->where(['create_id'=>$create_id])
            ->order('tung_num asc,unit_num asc')
            ->select();
        if($data!=null){
            foreach($data as $k=>$v){
                $data[$k]['unit']=$v['tung_num'].'-'.$v['unit_num'];
            }
        }
        //后台朝向数据
        $date=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->where(['create_id'=>$create_id])
            ->field('housing_oorientation')
            ->find();
        //住房配套
        $i=Db::name('settings')
            ->where(['apartment_state'=>1])
            ->where(['create_id'=>$create_id])
            ->field('housing_matching')
            ->find();
        //模板
        $m=Db::name('housing_template')
            ->where(['create_id'=>$create_id])
            ->field('template_id,te_name')
            ->where(['te_state'=>1])
            ->select();
        //经纪人
        $user_id=Db::name('role')
            ->where(['status'=>1])
            ->where(['create_id'=>$create_id])
            ->where(['name'=>'经纪人'])
            ->find();
       // var_dump($user_id['role_id'],$create_id);exit;
        $user=Db::name('admin_user')
            ->where(['role_id'=>$user_id['role_id']])
            ->where(['admin_status'=>1])
            ->where(['create_id'=>$create_id])
            ->field('admin_id,name')
            ->select();
       // var_dump($user);exit;
        //判断设置没有   没有返回2
        if($data!=null && $date!=null && $i!=null){
            $arr['orientation']=$date['housing_oorientation'];//朝向
            $arr['unit']=$data;//单元

            if($i['housing_matching']!=null){
                if($matching!=null){
                    $arr1= explode(',',$i['housing_matching']);
                    $arr2= explode(',',$matching);
                    $result=array_merge($arr1,$arr2);
                    $result= array_unique($result);
                    $result= implode(',',$result);
                }else{
                    $result= $i['housing_matching'];
                }
            }else{
                if($matching!=null){
                    $result=$matching;
                }else{
                    $result= $i['housing_matching'];
                }
            }

            $arr['matching']=$result;//配套
            $arr['user']=$user;//配套
            $arr['template']=$m;//配套
            return $arr;
        }else{
            return 2;
        }
    }
    /**
     * 住房房源添加
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingAdd($data){
        //处理数据
        $arr['h_tung_num'] = isset($data['h_tung_num']) ? trim($data['h_tung_num']) : "";
        $arr['h_layer_num'] = isset($data['h_layer_num']) ? trim($data['h_layer_num']) : "";
        $arr['h_agent'] = isset($data['h_agent']) ? trim($data['h_agent']) : "";
        $arr['h_room_number'] = isset($data['h_room_number']) ? trim($data['h_room_number']) : "";
        $arr['h_unit_num'] = isset($data['h_unit_num']) ? trim($data['h_unit_num']) : "";
        $arr['h_area'] = isset($data['h_area']) ? trim($data['h_area']) : "";
        $arr['h_root'] = isset($data['h_root']) ? trim($data['h_root']) : "";
        $arr['h_guest_room'] = isset($data['h_guest_room']) ? trim($data['h_guest_room']) : "";
        $arr['h_toilet'] = isset($data['h_toilet']) ? trim($data['h_toilet']) : "";
        $arr['h_floor_plans'] = isset($data['h_floor_plans']) ? implode(',',$data['h_floor_plans']) : "";
        $arr['h_indoor_drawings'] = isset($data['h_indoor_drawings']) ? implode(',',$data['h_indoor_drawings']) : "";
        $arr['h_money'] = isset($data['h_money']) ? trim($data['h_money']) : "";
        $arr['h_orientation'] = isset($data['h_orientation']) ? $data['h_orientation'] : "";
        $arr['h_matching'] = isset($data['h_matching']) ? implode(',',$data['h_matching']) : "";
        $arr['create_id'] =  create_id($data['token']);
        $arr['h_a_time'] = time();
       $datas=Db::name('housing_information')->insert($arr);
        //判断是否存为模板
        $data['h_template']= isset($data['h_template']) ? trim($data['h_template']) : "";
        if($data['h_template']==1){
            //处理数据
            //添加操作用户id
            $uid=get_user_id($data['token']);
            $count['te_uid'] = $uid;
            //$count['te_uid'] = 2;
            $count['te_name'] = isset($data['h_template_name']) ? trim($data['h_template_name']) : "";
            $count['te_area'] = isset($data['h_area']) ? trim($data['h_area']) : "";
            $count['te_money'] = isset($data['h_money']) ? trim($data['h_money']) : "";
            $count['te_root'] = isset($data['h_root']) ? trim($data['h_root']) : "";
            $count['te_guest_room'] = isset($data['h_guest_room']) ? trim($data['h_guest_room']) : "";
            $count['te_toilet'] = isset($data['h_toilet']) ? trim($data['h_toilet']) : "";
            $count['te_orientation'] = isset($data['h_orientation']) ? trim($data['h_orientation']) : "";
            $count['te_floor_plans'] = isset($data['h_floor_plans']) ? implode(',',$data['h_floor_plans']) : "";
            $count['te_indoor_drawings'] = isset($data['h_indoor_drawings']) ? implode(',',$data['h_indoor_drawings']) : "";
            $count['create_id'] =  create_id($data['token']);
            $count['te_matching'] = isset($data['h_matching']) ? implode(',',$data['h_matching']) : "";
            $count['te_a_time'] = time();
         //   var_dump($count);exit;
            $data=Db::name('housing_template')->insert($count);
            //var_dump($count);exit;
        }
        return $datas;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungs($data){
        //处理数据
        $i['h_state'] = 1;
        $where['h_tung_num'] = $data['h_tung_num'];
        $where['h_unit_num'] = $data['h_unit_num'];
        $where['h_layer_num'] = $data['h_layer_num'];
        $where['h_room_number'] = $data['h_room_number'];
        $where['create_id'] =  create_id($data['token']);
        $date=Db::name('housing_information')->where($i)->where($where)->find();
        return $date;
    }
    /**
     * 判断楼层房号是否重复
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function tungsUp($data){
        //处理数据
        $i['h_state'] = 1;
        $where['h_tung_num'] = $data['h_tung_num'];
        $where['h_unit_num'] = $data['h_unit_num'];
        $where['h_layer_num'] = $data['h_layer_num'];
        $where['h_room_number'] = $data['h_room_number'];
        $where['create_id'] =  create_id($data['token']);
        $wheres['housing_id'] = array('neq',$data['housing_id']);
        $date=Db::name('housing_information')->where($i)->where($wheres)->where($where)->find();
        return $date;
    }
    /**
     * 住房资源查询
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingOne($id){
        //处理数据
        $input = input();
        $data=Db::name('housing_information')
            ->alias('a')
            ->join('admin_user','a.h_agent=admin_user.admin_id')
            ->where(['housing_id'=>$id])
           // ->where(['create_id'=> create_id($input['token'])])
            ->where(['h_state'=>1])
            ->find();
        $data['h_floor_plans']=explode(',',$data['h_floor_plans']);
        $data['h_indoor_drawings']=explode(',',$data['h_indoor_drawings']);
        $data['matching']=$data['h_matching'];
        $data['h_matching']=explode(',',$data['h_matching']);

        return $data;
    }
    /**
     * 住房修改
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function unitUpdate($data){
        //处理数据
        $where['housing_id']=isset($data['housing_id']) ? trim($data['housing_id']) : "";
        $arr['h_agent'] = isset($data['h_agent']) ? trim($data['h_agent']) : "";
        $arr['h_tung_num'] = isset($data['h_tung_num']) ? trim($data['h_tung_num']) : "";
        $arr['h_unit_num'] = isset($data['h_unit_num']) ? trim($data['h_unit_num']) : "";
        $arr['h_layer_num'] = isset($data['h_layer_num']) ? trim($data['h_layer_num']) : "";
        $arr['h_money'] =  isset($data['h_money']) ? trim($data['h_money']) : "";
        $arr['h_room_number'] = isset($data['h_room_number']) ? trim($data['h_room_number']) : "";
        $arr['h_area'] = isset($data['h_area']) ? trim($data['h_area']) : "";
        $arr['h_root'] = isset($data['h_root']) ? trim($data['h_root']) : "";
        $arr['h_guest_room'] = isset($data['h_guest_room']) ? trim($data['h_guest_room']) : "";
        $arr['h_toilet'] = isset($data['h_toilet']) ? trim($data['h_toilet']) : "";
        $arr['h_floor_plans'] = isset($data['h_floor_plans']) ? implode(',',$data['h_floor_plans']) : "";
        $arr['h_indoor_drawings'] = isset($data['h_indoor_drawings']) ? implode(',',$data['h_indoor_drawings']) : "";
        $arr['h_orientation'] = isset($data['h_orientation']) ? trim($data['h_orientation']) : "";
        $arr['h_matching'] = isset($data['h_matching']) ? implode(',',$data['h_matching']) : "";
        $arr['h_u_time'] = time();
        //var_dump($arr);exit;
        $data=Db::name('housing_information')->where($where)->update($arr);
        return $data;
    }
    /**
     * 住房房源显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index(){
        $data = input();
        $page=isset($data['page']) ? trim($data['page']) : 1;
        $number=isset($data['h_room_number']) ? trim($data['h_room_number']) : '';
        $style=isset($data['status']) ? trim($data['status']) : '';
        $state=isset($data['type']) ? trim($data['type']) : '';
        $tung_num=isset($data['tung_num']) ? trim($data['tung_num']) : '';
        $unit_num=isset($data['unit_num']) ? trim($data['unit_num']) : '';
        $room_number=isset($data['room_number']) ? trim($data['room_number']) : '';
        $where=[];
        $where['h_state']=1;
        $where['create_id'] = create_id($data['token']);
        if($number!=null){
            $where['h_room_number']=$number;
        }
        if($tung_num!=null){
            $where['h_tung_num']=$tung_num;
        }
        if($unit_num!=null){
            $where['h_unit_num']=$unit_num;
        }
        if($room_number!=null){
            $where['h_room_number']=$room_number;
        }
        if($style!=null){
            $where['status']=$style;
        }
        if($state!=null){
            $where['lease']=$state;
        }
        $count=Db::name('housing_information')
            ->where($where)
            ->count();
        $data=Db::name('housing_information')
            ->where($where)
            ->page($page, 10)
            ->order("h_tung_num asc,h_unit_num asc,h_layer_num asc,h_room_number asc")
            ->select();
       foreach($data as $k=>$v){
            $data[$k]['unit']=$v['h_tung_num'].'-'.$v['h_unit_num'].'-'.$v['h_layer_num'].'-'.$v['h_room_number'];
            $data[$k]['room']=$v['h_root'].'室'.$v['h_guest_room'].'厅'.$v['h_toilet'].'卫';
            if($v['lease']==2){
                $arr['h_id']=$v['housing_id'];
                $arr['h_type']=1;
                $housing=Db::name('contracted_housing')->where($arr)->order('c_h_id desc')->find();
                $tenant=Db::name('tenant_contract')
                    ->alias('a')
                    ->join('tenant_management b','a.t_id=b.tenant_id')
                   ->where(['t_c_mid'=>$housing['c_c_mid']])
                    ->field('b.tenant_name')
                    ->where(['t_type'=>1])
                    ->find();
                $data[$k]['tenant_name']=$tenant['tenant_name'];//租客
            }
            //$data['count']=$count;
        }
        $date['data']=$data;
        $date['count']=$count;
       // $data['count']=$count;
        return $date;
    }
    /**
     * 住房房源显示
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function housingDel($data){
        $date['housing_id']=$data['housing_id'];
        $data=Db::name('housing_information')
            ->where($date)
            ->update(['h_state'=>2]);
        return $data;
    }

    /**
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function indexCron(){
        $where=[];
        $where['h_state']=1;
        $where['lease']=2;
        $data=Db::name('housing_information')
            ->field('housing_id, h_tung_num, h_unit_num, h_layer_num, h_room_number, h_agent, create_id')
            ->where($where)
            ->select();
        return $data;
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function indexLease($admin_id) {
        $where['lease'] = 2;
        $where['h_state'] = 1;
        $where['create_id'] = $admin_id;
        $data=Db::name('housing_information')
            ->where($where)
            ->select();
        return $data;
    }

    /**
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id) {
        $where['housing_id'] = $id;
        $where['h_state'] = 1;
        $data = Db::name('housing_information')
            ->where($where)
            ->find();
        return $data;
    }
}